package com.taotao.manage.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.github.abel533.entity.Example;
import com.github.abel533.mapper.Mapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.manage.pojo.BasePojo;


public class BaseService<T extends BasePojo> {
	@Autowired
	// spring4属性注入
	public Mapper<T> mapper;

    // 当前Service上泛型的字节码对象
    private Class<T> clazz;

	/*
	 * 查询所有参数为null查询所有
	 */
	public List<T> queryAll() {
		return this.mapper.select(null);

	}

	/*
	 * 通用方法,根据主键查对象
	 */
	public T queryById(Object id) {

		return this.mapper.selectByPrimaryKey(id);

	}

	/**
	 * 查询单个条件对象
	 * 
	 * @param record
	 * @return
	 */
	public T queryOne(T record) {
		return this.mapper.selectOne(record);

	}

	/**
	 * 根据条件查询多个对象
	 * 
	 * @param record
	 * @return
	 */
	public List<T> queryByListWhere(T record) {
		return this.mapper.select(record);

	}

	// queryById
	// 2、 queryAll
	// 3、 queryOne
	// 4、 queryListByWhere
	// 5、 queryPageListByWhere
	// 6、 save
	// 7、 update
	// 8、 deleteById
	// 9、 deleteByIds
	// 10、deleteByWhere
	/**
	 * 分页查询
	 * 
	 * @param record
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageInfo<T> queryPageListByWhere(T record, Integer pageNum,
			Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<T> list = this.mapper.select(record);
		return new PageInfo<>(list);

	}
	
	/**
	 * 分页查询后顺次
	 * 
	 * @param record
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageInfo<T> queryPageListAndSort(String orderByClause , Integer page,
			Integer rows) {
		//开启分页
		PageHelper.startPage(page, rows);
		//构建查寻条件
		Example example = new Example(clazz);
		//排序
		example.setOrderByClause(orderByClause);
		//查询
		List<T> list = this.mapper.selectByExample(example);
		//返回分页对象
		return new PageInfo<>(list);
		
	}

	/**
	 * 保存数据
	 * 
	 * @param record
	 * @return
	 */
	public boolean save(T record) {
		record.setCreated(new Date());
		record.setUpdated(record.getCreated());

		return this.mapper.insert(record)==1;

	}

	/**
	 * 根据这件更新数据
	 * 
	 * @param record
	 * @return true(成功) false(失败)
	 */
	public boolean update(T record) {
		record.setCreated(new Date());
		return this.mapper.updateByPrimaryKey(record) == 1;

	}
	

	/**
	 * 根据主键删除
	 * 
	 * @param record
	 * @return
	 */
	public boolean deleteById(Object id) {
		return this.mapper.deleteByPrimaryKey(id) == 1;
	}

	/**
	 * 根据主键id的集合删除多个对象
	 * 
	 * @param id
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public Integer deleteByIds(Class clazz, String property, List<Object> ids) {

		Example example = new Example(clazz);
		example.createCriteria().andIn(property, ids);
		return this.mapper.deleteByExample(example);
	}

	/**
	 * 根据条件删除对象
	 * 
	 * @param record
	 * @return
	 */
	public Integer deleteByWhere(T record) {
		return this.mapper.delete(record);
	}

	/**
	 * 根据主键更新对象
	 * @param record
	 * @return
	 */
	public Boolean updateByPrimaryKey(T record){
		//设置更新时间
		record.setUpdated(new Date());
		return this.mapper.updateByPrimaryKeySelective(record) == 1;
	}
	
}
